﻿using System;
using System.Data;
using System.Collections.Generic;
using eTMS.Controllers.DanhSachCamThiService;
namespace eTMS.Controllers
{
    public class DanhSachCamThiControllerBase : CommonController
    {
        public DanhSachCamThiControllerBase()
            : base()
        {
            this.dao = new DanhSachCamThiServiceClient();
        }
        ~DanhSachCamThiControllerBase()
        {
            if(dao.State==System.ServiceModel.CommunicationState.Opened)try{dao.Close();}catch{}
        }
        private DataTable _Table;
        private DanhSachCamThiEntity oDanhSachCamThi;
        public DanhSachCamThiEntity DanhSachCamThiObject
        {
            get
            {
                return oDanhSachCamThi;
            }
            set
            {
                oDanhSachCamThi = value;
            }
        }
        protected DanhSachCamThiServiceClient dao;
        public DataTable Table
        {
            get
            {
                return _Table;
            }
            set
            {
                _Table = value;
            }
        }

        public void AddIndexToTable()
        {
            this.AddIndexToTable(_Table);
        }
        public void ResetTableIndex()
        {
            this.ResetTableIndex(_Table);
        }
        public bool AddNew()
        {
            if (oDanhSachCamThi == null) return false;
            bool result = true;
            try
            {
                oDanhSachCamThi = dao.SaveRet(oDanhSachCamThi);
            }
            catch
            {
                result = false;
            }
            if (result)
            {
                _ListDanhSachCamThi.Add(oDanhSachCamThi);
            }
            return result;
        }
        public bool Update()
        {
            if (oDanhSachCamThi == null) return false;
            return dao.Update(oDanhSachCamThi);
        }
        public bool Delete()
        {
            if (oDanhSachCamThi == null) return false;
            bool retvalue = dao.Delete(oDanhSachCamThi);
            if (retvalue)
            {
                _ListDanhSachCamThi.Remove(oDanhSachCamThi);
            }
            return retvalue;
        }
        public bool DeleteByID(Int32 DanhSachCamThi_ID)
        {
            bool retvalue = dao.DeleteByID(DanhSachCamThi_ID);
            if (retvalue)
            {
                DanhSachCamThiEntity oDeleted = FindDanhSachCamThi(DanhSachCamThi_ID);
                if (oDeleted != null)
                {
                    _ListDanhSachCamThi.Remove(oDeleted);
                }
            }
            return retvalue;
        }
        public void ResetObject()
        {
            this.oDanhSachCamThi = new DanhSachCamThiEntity();
        }
        //Copy data from data row in grid to object
        public DanhSachCamThiEntity MapDataRowToObject(DataRow row)
        {
            DanhSachCamThiEntity o = new DanhSachCamThiEntity();
            if (!row.IsNull("MonHoc_ID"))
                o.MonHoc_ID = ConvertInt(row["MonHoc_ID"]);
            if (!row.IsNull("HocKy_ID"))
                o.HocKy_ID = (Int32)(row["HocKy_ID"]);
            if (!row.IsNull("LyDoCamThi_ID"))
                o.LyDoCamThi_ID = (Int32)(row["LyDoCamThi_ID"]);
            if (!row.IsNull("NamHoc_ID"))
                o.NamHoc_ID = (Int32)(row["NamHoc_ID"]);
            if (!row.IsNull("SinhVien_ID"))
                o.SinhVien_ID = (Int32)(row["SinhVien_ID"]);
            if (!row.IsNull("DanhSachCamThi_ID"))
                o.DanhSachCamThi_ID = (Int32)(row["DanhSachCamThi_ID"]);
            return o;
        }
        //Copy data from object to data row in grid    
        public void MapObjectToDataRow(DanhSachCamThiEntity o, DataRow row)
        {
            row["MonHoc_ID"] = o.MonHoc_ID;
            row["HocKy_ID"] = o.HocKy_ID;
            row["LyDoCamThi_ID"] = o.LyDoCamThi_ID;
            row["NamHoc_ID"] = o.NamHoc_ID;
            row["SinhVien_ID"] = o.SinhVien_ID;
            row["DanhSachCamThi_ID"] = o.DanhSachCamThi_ID;
        }
        public void MapObjectToDataRow(DataRow row)
        {
            this.MapObjectToDataRow(this.DanhSachCamThiObject, row);
        }
        public void LoadObject(Int32 DanhSachCamThi_ID)
        {
            DanhSachCamThiEntity oLoad = FindDanhSachCamThi(DanhSachCamThi_ID);
            if (oLoad == null)
            {
                oDanhSachCamThi = this.LoadDanhSachCamThi(DanhSachCamThi_ID);
                bool bNotIsNull = true;
                bNotIsNull &= oDanhSachCamThi.IsNullDanhSachCamThi_ID;
                if (!bNotIsNull)
                {
                    this.ListDanhSachCamThi.Add(oDanhSachCamThi);
                }
            }
            else
            {
                oDanhSachCamThi = oLoad;
            }
        }
        #region CachedList DanhSachCamThi
        private static List<DanhSachCamThiEntity> _ListDanhSachCamThi = new List<DanhSachCamThiEntity>();
        public List<DanhSachCamThiEntity> ListDanhSachCamThi
        {
            get
            {
                return _ListDanhSachCamThi;
            }
        }
        public static List<DanhSachCamThiEntity> ListDanhSachCamThiStatic
        {
            get
            {
                return _ListDanhSachCamThi;
            }
        }
        public DanhSachCamThiEntity FindDanhSachCamThi(Int32 DanhSachCamThi_ID)
        {
            DanhSachCamThiEntity oResult = this.ListDanhSachCamThi.Find(delegate(DanhSachCamThiEntity oFind)
            {
                bool result = true;
                result &= oFind.DanhSachCamThi_ID.Equals(DanhSachCamThi_ID);
                return result;
            });
            return oResult;
        }
        public static void LoadAllDanhSachCamThi(bool clearCache)
        {
            if (_ListDanhSachCamThi.Count == 0)
                clearCache = true;
            if (!clearCache) return;
            DanhSachCamThiServiceClient dao = new DanhSachCamThiServiceClient();
            _ListDanhSachCamThi = new List<DanhSachCamThiEntity>(dao.SelectAll());
            if(dao.State==System.ServiceModel.CommunicationState.Opened)try{dao.Close();}catch{}
        }
        public DanhSachCamThiEntity LoadDanhSachCamThi(Int32 DanhSachCamThi_ID)
        {
            DanhSachCamThiEntity o = dao.SelectOne(DanhSachCamThi_ID);
            return o;
        }
        #endregion
    }
}
